/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <kafka/config.h>
#include <kafka/kafka_core.h>
#include <kafka/kafka_comm.h>

extern int kafka_get_short(Frbyte_Packet *p_st_data, int *p_i_len);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = FR_SUCC;
    int i_out = 0;
    unsigned int ui_len = 100;
    char *p_data = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_data = (char *) frmalloc(ui_len);
    if (p_data == NULL)
    {
        printf("frmalloc failed!\n");
        return;
    }
    memset(p_data, 0x0, ui_len);
    memcpy(p_data, "\x00", 1);

    p_st_data = bytebuff_creat();
    if (p_st_data == NULL)
    {
        frfree(p_data);
        printf("bytebuff_creat failed!\n");
        return;
    }

    i_ret = bytebuff_add(p_st_data, p_data, 1);
    if (i_ret != FR_SUCC)
    {
        frfree(p_data);
        bytebuff_free(&p_st_data);
        printf("bytebuff_add failed!\n");
        return;
    }
    bytebuff_skip(p_st_data, 1);

    i_ret = kafka_get_short(p_st_data, &i_out);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    frfree(p_data);
    bytebuff_free(&p_st_data);
    return;
}

static void __case2(void)
{
    int i_ret = FR_SUCC;
    int i_out = 0;
    unsigned int ui_len = 100;
    char *p_data = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_data = (char *) frmalloc(ui_len);
    if (p_data == NULL)
    {
        printf("frmalloc failed!\n");
        return;
    }
    memset(p_data, 0x0, ui_len);
    memcpy(p_data, "\x00", 1);

    p_st_data = bytebuff_creat();
    if (p_st_data == NULL)
    {
        frfree(p_data);
        printf("bytebuff_creat failed!\n");
        return;
    }

    i_ret = bytebuff_add(p_st_data, p_data, 1);
    if (i_ret != FR_SUCC)
    {
        frfree(p_data);
        bytebuff_free(&p_st_data);
        printf("bytebuff_add failed!\n");
        return;
    }
    bytebuff_skip(p_st_data, 1);

    i_ret = kafka_get_short(p_st_data, &i_out);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    frfree(p_data);
    bytebuff_free(&p_st_data);
    return;
}

static void __case3(void)
{
    int i_ret = FR_SUCC;
    int i_out = 0;
    unsigned int ui_len = 100;
    char *p_data = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_data = (char *) frmalloc(ui_len);
    if (p_data == NULL)
    {
        printf("frmalloc failed!\n");
        return;
    }
    memset(p_data, 0x0, ui_len);
    memcpy(p_data, "\x00", 1);

    p_st_data = bytebuff_creat();
    if (p_st_data == NULL)
    {
        frfree(p_data);
        printf("bytebuff_creat failed!\n");
        return;
    }

    i_ret = bytebuff_add(p_st_data, p_data, 1);
    if (i_ret != FR_SUCC)
    {
        frfree(p_data);
        bytebuff_free(&p_st_data);
        printf("bytebuff_add failed!\n");
        return;
    }

    i_ret = kafka_get_short(p_st_data, &i_out);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    frfree(p_data);
    bytebuff_free(&p_st_data);
    return;
}

static void __case4(void)
{
    int i_ret = FR_SUCC;
    int i_out = 0;
    unsigned int ui_len = 100;
    char *p_data = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_data = (char *) frmalloc(ui_len);
    if (p_data == NULL)
    {
        printf("frmalloc failed!\n");
        return;
    }
    memset(p_data, 0x0, ui_len);
    memcpy(p_data, "\x00\x01", 2);

    p_st_data = bytebuff_creat();
    if (p_st_data == NULL)
    {
        frfree(p_data);
        printf("bytebuff_creat failed!\n");
        return;
    }

    i_ret = bytebuff_add(p_st_data, p_data, 2);
    if (i_ret != FR_SUCC)
    {
        frfree(p_data);
        bytebuff_free(&p_st_data);
        printf("bytebuff_add failed!\n");
        return;
    }

    i_ret = kafka_get_short(p_st_data, &i_out);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(bytebuff_remaining(p_st_data), 0);
    CU_ASSERT_EQUAL(i_out, 1);

    frfree(p_data);
    bytebuff_free(&p_st_data);
    return;
}

static void __case5(void)
{
    int i_ret = FR_SUCC;
    int i_out = 0;
    unsigned int ui_len = 100;
    char *p_data = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_data = (char *) frmalloc(ui_len);
    if (p_data == NULL)
    {
        printf("frmalloc failed!\n");
        return;
    }
    memset(p_data, 0x0, ui_len);
    memcpy(p_data, "\x00\x01\x00\x01", 4);

    p_st_data = bytebuff_creat();
    if (p_st_data == NULL)
    {
        frfree(p_data);
        printf("bytebuff_creat failed!\n");
        return;
    }

    i_ret = bytebuff_add(p_st_data, p_data, 4);
    if (i_ret != FR_SUCC)
    {
        frfree(p_data);
        bytebuff_free(&p_st_data);
        printf("bytebuff_add failed!\n");
        return;
    }

    i_ret = kafka_get_short(p_st_data, &i_out);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(bytebuff_remaining(p_st_data), 2);
    CU_ASSERT_EQUAL(i_out, 1);

    frfree(p_data);
    bytebuff_free(&p_st_data);
    return;
}

int add_suite_kafka_get_short(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" kafka_get_short()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
